package com.zhangzhan.gulimallware.controller;

import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.R;
import com.zhangzhan.gulimallware.entity.PurchaseEntity;
import com.zhangzhan.gulimallware.service.PurchaseService;
import com.zhangzhan.gulimallware.vos.PurchaseDoneVo;
import com.zhangzhan.gulimallware.vos.PurchaseMergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 采购信息
 *
 * @author zhangzhan
 * @email sunlightcs@gmail.com
 * @date 2024-06-18 23:32:09
 */
@RestController
@RequestMapping("gulimallware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 查询未领取的采购单 /ware/purchase/unreceive/list
     */
    @RequestMapping("/unreceive/list")
    //@RequiresPermissions("gulimallware:purchase:list")
    public R unreceiveList(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPageUnreceiveList(params);

        return R.ok().put("page", page);
    }

    //    /api/gulimallware/purchase/merge
    @RequestMapping(value = "/merge",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallware:purchase:list")
    public R merge(@RequestBody PurchaseMergeVo vo) {
        purchaseService.merge(vo);

        return R.ok();
    }


    //    /ware/purchase/received 领取采购单
    @RequestMapping(value = "/received",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallware:purchase:list")
    public R received(@RequestBody List<Long> purchaseIds) {
        purchaseService.received(purchaseIds);

        return R.ok();
    }

    ///ware/purchase/done
    @RequestMapping(value = "/done",method = RequestMethod.POST)
    //@RequiresPermissions("gulimallware:purchase:list")
    public R done(@RequestBody PurchaseDoneVo vo) {
        purchaseService.done(vo);

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("gulimallware:purchase:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("gulimallware:purchase:info")
    public R info(@PathVariable("id") Long id) {
        PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("gulimallware:purchase:save")
    public R save(@RequestBody PurchaseEntity purchase) {
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
        purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("gulimallware:purchase:update")
    public R update(@RequestBody PurchaseEntity purchase) {
        purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("gulimallware:purchase:delete")
    public R delete(@RequestBody Long[] ids) {
        purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
